﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WLMCoreLibrary;
using SAW_Core;

namespace SAW
{
    public partial class AdministracionMenu : System.Web.UI.Page
    {
        public string[] SKIN; dbConnector ConexionDB = new dbConnector();
        protected override void OnInit(EventArgs e)
        {
            ConexionDB.OpenConnection(); base.OnInit(e); FuncionesLocales.NoCache();

        } protected override void OnUnload(EventArgs e) { ConexionDB.CloseConnection(); base.OnUnload(e); }
        protected void Page_Load(object sender, EventArgs e)
        {            //WLMFunctions.GrantAccess(ref ConexionDB);
            FuncionesLocales.NoCache();
          if (!IsPostBack)
            {

                RibbonMenu cat = new RibbonMenu(false,ref ConexionDB);
                TreeNodeCollection rama = new TreeNodeCollection();
                rama = cat.CreateTreeNodes(0, 0, "$('idPadre').value=idNodo;", 0, "Categorias");
                foreach (TreeNode nodo in rama)
                    ArbolCategorias.Nodes.Add(nodo);

            }

        }

        protected void add_Click(object sender, EventArgs e)
        {
            string ruta = Server.MapPath("");
            if (FileUpload1.FileName != "")
                FileUpload1.SaveAs(ruta + "/Ribbon/images/" + FileUpload1.FileName);

            RibbonMenu opcionMenu = new RibbonMenu(true,ref ConexionDB);
            opcionMenu.Caption = caption.Text;
            opcionMenu.Descripcion = descripcion.Text;
            opcionMenu.Relacion = relacion.Text;
            opcionMenu.orden = Convert.ToInt32(orden.Text);
            opcionMenu.idParent = Convert.ToInt32(ArbolCategorias.SelectedValue);
            if (FileUpload1.FileName != "")
                opcionMenu.Icono = "/Ribbon/Images/" + FileUpload1.FileName;
            opcionMenu.OnClick = onclick.Text;
            opcionMenu.OnOut = onout.Text;
            opcionMenu.OnOver = onover.Text;

            opcionMenu.Save();

            Response.Redirect("AdministracionMenu.aspx?ajax=1");
        }

        protected void edit_Click(object sender, EventArgs e)
        {

            EditarOpcionMenu(Convert.ToInt32(ArbolCategorias.SelectedValue));

            Response.Redirect("AdministracionMenu.aspx?ajax=1");
        }

        protected void ArbolCategorias_SelectedNodeChanged(object sender, EventArgs e)
        {
            RibbonMenu categoria = new RibbonMenu(Convert.ToInt32(ArbolCategorias.SelectedValue),ref ConexionDB);

            descripcion.Text = categoria.Descripcion;
            orden.Text = categoria.orden.ToString();

        }

        protected void del_Click(object sender, EventArgs e)
        {
            RibbonMenu categoria = new RibbonMenu(Convert.ToInt32(ArbolCategorias.SelectedValue),ref ConexionDB);
            categoria.Delete();
            Application.Remove("MenuPrincipal");
            Application.Remove("MenuInferior");
            Response.Redirect("AdministracionMenu.aspx?ajax=1");
        }

        protected void arbolito_SelectedNodeChanged(object sender, EventArgs e)
        {

        }

        protected void ArbolCategorias_SelectedNodeChanged1(object sender, EventArgs e)
        {


            int idRegistroSeleccionado = Convert.ToInt32(ArbolCategorias.SelectedNode.Value);
            RibbonMenu opcion = new RibbonMenu
        (idRegistroSeleccionado,ref ConexionDB);
            caption.Text = opcion.Caption;
            if (opcion.Icono != "")
            {
                iconImage.Visible = true;
                iconImage.ImageUrl = opcion.Icono;
            }
            descripcion.Text = opcion.Descripcion;
            relacion.Text = opcion.Relacion;
            orden.Text = opcion.orden.ToString();
            onover.Text = opcion.OnOver;
            onclick.Text = opcion.OnClick;
            onout.Text = opcion.OnOut;
            opcion.Dispose();
        }

        protected string EditarOpcionMenu(int idOpcion)
        {
            string ruta = Server.MapPath("");
            if (FileUpload1.FileName != "")
                FileUpload1.SaveAs(ruta + "/images/" + FileUpload1.FileName);
            string salida = "";
            RibbonMenu opcionMenu = new RibbonMenu(idOpcion,ref ConexionDB);

            salida = opcionMenu.Caption.ToString();
            opcionMenu.Caption = caption.Text;
            opcionMenu.Descripcion = descripcion.Text;
            opcionMenu.Relacion = relacion.Text;
            opcionMenu.orden = Convert.ToInt32(orden.Text);
            if (FileUpload1.FileName != "")
                opcionMenu.Icono = "/images/" + FileUpload1.FileName;
            opcionMenu.OnClick = onclick.Text;
            opcionMenu.OnOut = onout.Text;
            opcionMenu.OnOver = onover.Text;
            opcionMenu.Save();
            Application.Remove("MenuPrincipal");
            Application.Remove("MenuInferior");
            return salida;
        }

    }
}